package edu.stanford.bmir.protege.web.client.ui.ontology.transform;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.gwtext.client.widgets.MessageBox;

import edu.stanford.bmir.protege.web.client.ui.util.UIUtil;

public class StepOne extends Composite {
	
	@UiField
	FormPanel formPanel;
	
	@UiField
	FileUpload fileUpload;
	
	@UiField
	RadioButton txt;
	
	@UiField 
	RadioButton xml;
	
	@UiField
	Hidden type;
	
	@UiField
	Hidden fileName;
	
	@UiField
	Button button;
	
	@UiField
	HTMLPanel hPanel;
	
	@UiField
	TextBox iri;
	
	MainView view;

	private static StepOneUiBinder uiBinder = GWT.create(StepOneUiBinder.class);

	interface StepOneUiBinder extends UiBinder<Widget, StepOne> {
	}

	public StepOne( final MainView view ) {
		initWidget(uiBinder.createAndBindUi(this));
		
		this.view = view;
		iri.setText("http://");
		formPanel.setAction(GWT.getModuleBaseURL() + "fileUpload");
		formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
		formPanel.setMethod(FormPanel.METHOD_POST);
		
		txt.addClickHandler(new RadionClickHandle());
		xml.addClickHandler(new RadionClickHandle());
		
		formPanel.addSubmitCompleteHandler( new SubmitCompleteHandler() {
			@Override
			public void onSubmitComplete(SubmitCompleteEvent event) {
				
				UIUtil.hideLoadProgessBar();
				
				String result = event.getResults().replace( "<pre>" , "");
				result = result.replace( "</pre>" , "");
				
				if ( "error".equals( result ) ) {
					
					MessageBox.alert("文件解析错误，请核对文件内容是否正确！");
					
				} else {
					
					view.setFileName(result);
					
					System.out.println( result );
					
					view.openStepTwo();
				}
			}
		} );
		
		button.addClickHandler( new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				
				String con = type.getValue();
				
				if ( con == null || con == "" ) {
					type.setValue( "txt" );
				}
				Long time =  new Date().getTime();
				fileName.setValue(String.valueOf( time ));
				
				String name = fileUpload.getFilename();
				
				if ( name == null || name == "" ) {
					MessageBox.alert("请选择上传文件！");  
				} if ( !name.endsWith( type.getValue() )  ) {
					MessageBox.alert("请上传格式正确的文件！"); 
				} else {
					UIUtil.showLoadProgessBar("上传", "上传文件中,请稍后 ");
					//将表单地址加上IRI传入后台
					String IRIpath ="";
					String text = iri.getText();
					if(text!=null&&text.startsWith("http://")){
						if(!text.endsWith("/")){
							text+="/";
						}
						IRIpath="?IRI="+text;
					//传递IRI到后台
					}
					formPanel.setAction(GWT.getModuleBaseURL() + "fileUpload"+IRIpath);
					formPanel.submit();
				}
				
			}
		} );
		
	}
	
	private class RadionClickHandle implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			
			if ( txt.getValue() ) {
				type.setValue( "txt" );
			} else {
				type.setValue( "xml" );
			}
		}
		
	}

}
